<!--
**********************************************************
* @Author: 张小平
* @Date:   2024-05-01
* @Email:  qqshuqian@163.com
* @Last modified: 最后修改日期
* @Description: 文件描述
**********************************************************
-->
<template>
  <basic-container>
  <el-container>
    <el-aside width="48%" style="margin-right: 10px;">
      <avue-crud
              v-model="form"
              v-model:search="searchForm"
              v-model:page="page"
              :permission="getPermission"
              v-bind="bindVal"
              v-on="onEvent"
              :option="option"
              :data="tableData"
              @row-click="handleItem"
              :before-open="beforeOpen"
      >

      </avue-crud>
    </el-aside>
    <el-main width="48%" >
      <avue-crud
              ref="itemCrud"
              v-model:page="itemPage"
              :data="tableItemData"
              v-model:search="searchForm2"
              :option="tableItemOption"
              @row-save="handleItemSave"
              @row-update="handleItemUpdate"
              @row-del="handleItemDel"
              @size-change="itemSizeChange"
              @current-change="itemCurrentChange"

      />
    </el-main>
  </el-container>
</basic-container>
</template>



<script>
import {tableOption, tableItemOption} from "./dict.js";
import crud from "@/framework-core/mixins/crud";
import {vaildData} from "@/framework-core/utils/validate";
import {pageCore, listCore, getCore, saveCore,delCore,importCore,exportCore} from "@/framework-core/api/core/api";
import website  from '@/framework-core/config/website';

export default {
  name: "Dict",
  mixins: [crud],
  data() {
    return {
      //字典
      pageConfig:{"url":"sys/dic","sort":"sort","delProp":"name"},
      option: tableOption,
      //向字典项传值
      parentCode: undefined,
      parentId: undefined,
      searchForm2:{},
      //字典项
      itemForm: {},
      dialogFormVisible: false,
      tableItemOption: tableItemOption,
      tableItemData: [],
      itemPage: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: website.pageSize // 每页显示多少条
      }
    };
  },
  computed: {
    ...mapState({
      permissions: (state) =>state.user.permission,
    })
  },
  methods: {
    getPermission: function (key, row, index) {

      if (index >= 0 && "code" === key) {
        return !(vaildData(row.canDo && this.permissions[this.pageConfig.url.replace("/", "_") + "_save_post"]));
      }

      return this.getCommPermission(key, row, index);
    },
    beforParam: function (obj) {
      obj.parentId = 0;
    },
    beforeOpen: function (show, type) {
      show();
    },
    beforeClose: function (done, type) {
      done();
    },

    handleItem: function (row) {
      this.parentId = row.id;
      this.parentCode = row.code;
      this.getDictItemList(row.id);
    },

    //======字典项表格相关=====
    getDictItemList(parentId) {
      this.dialogFormVisible = true;
      pageCore(this.pageConfig.url,
              this.getParamItemSearch(parentId)
      ).then(response => {
        this.tableItemData = response.data.data.records;
        this.itemPage.total = response.data.data.total;
      });
    },

    getParamItemSearch(parentId) {
      debugger
      var obj = {
        pageNum: this.itemPage.currentPage,
        pageSize: this.itemPage.pageSize
      };
      obj.entity = this.searchForm2;
      obj.entity.parentId = parentId;
      return obj;
    },

    handleItemSave: function (row, done) {
      row.parentId =  this.parentId;
      saveCore(this.pageConfig.url, row).then(() => {
        this.$message.success("添加成功");
        this.getDictItemList(this.parentId);
        done();
      });
    },
    handleItemUpdate: function (row, index, done) {
      var $that = this;
      saveCore(this.pageConfig.url, row).then(() => {
        $that.$message.success("修改成功");
        $that.getDictItemList(this.parentId);
        done();
      });
    },
    handleItemDel: function (row) {
      var $that = this;
      this.$confirm('是否确认删除数据为"' + row.name + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
              .then(function () {
                return delCore($that.pageConfig.url, row);
              })
              .then(() => {
                this.getDictItemList($that.parentId);
                this.$message.success("删除成功");
              })
              .catch(function () {
              });
    },
    itemSizeChange(pageSize) {
      this.itemPage.pageSize = pageSize;
      this.getDictItemList(this.parentId);
    },
    itemCurrentChange(current) {
      this.itemPage.currentPage = current;
      this.getDictItemList(this.parentId);
    },
    // itemBeforeOpen: function (done, type) {
    //   debugger
    //   if (type == "add") {
    //     this.$set(this.$refs.itemCrud.tableForm, "parentId", this.parentId)
    //     this.$set(this.$refs['itemCrud'].tableForm, "parentCode", this.parentCode)
    //   }
    //   if (type == "edit") {
    //
    //   }
    //   done();
    // },
    // itemBeforeClose: function (done, type) {
    //   done();
    // },
  }
};
</script>

<style lang="scss" scoped></style>
